package com.aaa.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.aaa.annocations.FieldBasicVerify;
import com.aaa.exceptions.CustomException;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;


/**
 * @author : luoBo
 */
@Slf4j
public class FieldBasicVerifyUtil {

    public static void verifyFieldIsNotEmpty(Object obj){
        Arrays.stream(obj.getClass().getFields()).forEach(field->{
            FieldBasicVerify fieldBasicVerify = field.getAnnotation(FieldBasicVerify.class);
            Object fieldValue = ReflectUtil.getFieldValue(obj, field);
            if (ObjectUtil.isNull(obj)) {
                throw new CustomException(fieldBasicVerify.fieldName()+"字段不能为空");
            }
            if (obj instanceof String && StrUtil.isNotBlank((String) obj)) {
                throw new CustomException(fieldBasicVerify.fieldName()+"字段不能为空");
            }
        });
    }
}
